<template>
  <div class='top-category'>
    <div class="container">
      <!-- 面包屑组件 -->
      <XtxBread seperator=">">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>{{breadName}}</XtxBreadItem>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxSlider :sliders="sliders" :shows="true"></XtxSlider>
      <!-- 分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in categroyList" :key="item.id">
           <a href="javascript:;">
            <img :src="item.picture" >
            <p>{{item.name}}</p>
           </a>
           </li>
        </ul>
      </div>
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="i in categroyList" :key="i">
        <div class="head">
          <h3>-  {{i.name}}  -</h3>
          <!-- <XtxMore to="/" /> -->
        </div>
        <div class="body">
          <RouterLink to="/" class="goods-item" v-for="good in i.goods" :key="good.id">
            <img
             :src="good.picture"
             alt=""
            />
            <p class="name ellipsis">{{good.desc}}</p>
            <p class="desc ellipsis">{{good.name}}</p>
            <p class="price">&yen;{{good.price}}</p>
          </RouterLink>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findBanner } from '@/api/home'
import { findTopCategory } from '@/api/category'
import { onMounted, ref } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
export default {
  name: 'TopCategory',
  setup () {
    const sliders = ref([])
    async function loadList () {
      const res = await findBanner()
      sliders.value = res.result
    }
    onMounted(() => {
      loadList()
    })
    const categroyList = ref([])
    const route = useRoute()
    const breadName = ref('')
    async function loadCategroyList (id = route.params.id) {
      const res = await findTopCategory(id)
      categroyList.value = res.result.children
      breadName.value = res.result.name
    }
    onMounted(() => {
      loadCategroyList()
    })
    onBeforeRouteUpdate((to) => {
      // to指的是目标路由对象 to拿到最新的路由参数id
      // 使用最新id获取数据
      loadCategroyList(to.params.id)
    })
    return {
      sliders,
      categroyList,
      breadName
    }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
  margin-top:20px;
  background-color: #fff;
  .head {
    position: relative;
    .more {
      position: absolute;
      right: 20px;
      top: 30px;
    }
  }
  .body {
    display: flex;
    justify-content: space-around;
    padding: 0 20px;
    .goods-item {
      display: block;
      width: 220px;
      padding: 20px 30px;
      text-align: center;
      img {
        width: 160px;
        height: 160px;
      }
      p {
        padding-top: 10px;
      }
      .name {
        font-size: 16px;
      }
      .desc {
        color: #999;
        height: 29px;
      }
      .price {
        color: @priceColor;
        font-size: 20px;
      }
    }
  }
  }
  .box{
    .xtx-slider{
      .left{
        //  display: none;
        left: 0px;
      }
    }

  }
}
</style>
